﻿using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace EmoneyWeiXinDAL
{
    public class ModelMessagesDAL
    {
        //获取100条未发送条状态的的模板消息
        public List<ModelMessagesEntity> GetTop100NotSendList()
        {
            string sql = @"  SELECT top 100 [Id]
                            ,[weixinId]
                            ,[statu]
                            ,[firstText]
                            ,[keyNote1]
                            ,[keyNote2]
                            ,[keyNote3]
                            ,[keyNote4]
                            ,[keyNote5]
                            ,[keyNote6]
                            ,[keyNote7]
                            ,[keyNote8]
                            ,[touser]
                            ,[template_id]
                            ,[url]
                            ,[topcolor]
                            ,[remarkText]
                            , EarliestTime
                            ,[sendTime]
                            ,[addTime]
                        FROM [ModelMessages] 
                        where statu=0 AND EarliestTime<GETDATE() order by addTime desc ";
            return SQLHelper.GetDataByColumnsLst<ModelMessagesEntity>(Config.EmoneyWeiXin, sql, null);
        }

        public int UpdateStatus(int id, int statu)
        {
            string sql = "UPDATE ModelMessages SET Statu=@statu WHERE id=@id ";
            if (statu == 2)
            {
                sql = "UPDATE ModelMessages SET Statu=@statu,sendTime=GETDATE() WHERE id=@id";
            }
            SqlParameter[] paras =
            {
              new SqlParameter("@id",id),
              new SqlParameter("@statu",statu)
            };
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, paras);
        }

        public int Insert(ModelMessagesEntity entity)
        {
            string sql = @"INSERT INTO ModelMessages VALUES(@weixinId,@statu,@firstText,@keyNote1,@keyNote2,@keyNote3,@keyNote4,@keyNote5,@keyNote6,@keyNote7,@keyNote8,@touser,@template_id,@url,@topcolor,@remarkText,@EarliestTime,@sendTime,@addTime)";
            sql = @"INSERT INTO [EmoneyWeiXin].[dbo].[ModelMessages]
           ([weixinId]
           ,[statu]
           ,[firstText]
           ,[keyNote1]
           ,[keyNote2]
           ,[keyNote3]
           ,[keyNote4]
           ,[keyNote5]
           ,[keyNote6]
           ,[keyNote7]
           ,[keyNote8]
           ,[touser]
           ,[template_id]
           ,[url]
           ,[topcolor]
           ,[remarkText]
           ,[EarliestTime]
           ,[sendTime]
           ,[addTime])
     VALUES
           (@weixinId,@statu,@firstText,@keyNote1,@keyNote2,@keyNote3,@keyNote4,@keyNote5,@keyNote6,@keyNote7,@keyNote8,@touser,@template_id,@url,@topcolor,@remarkText,@EarliestTime,GETDATE(),GETDATE())";
            SqlParameter[] paras = 
            {
              new SqlParameter("@weixinId",entity.weixinId),
              new SqlParameter("@statu",entity.statu),
              new SqlParameter("@firstText",entity.firstText),
              new SqlParameter("@keyNote1",entity.keyNote1),
              new SqlParameter("@keyNote2",entity.keyNote2),
              new SqlParameter("@keyNote3",entity.keyNote3),
              new SqlParameter("@keyNote4",entity.keyNote4),
              new SqlParameter("@keyNote5",entity.keyNote5),
              new SqlParameter("@keyNote6",entity.keyNote6),
              new SqlParameter("@keyNote7",entity.keyNote7),
              new SqlParameter("@keyNote8",entity.keyNote8),
              new SqlParameter("@touser",entity.touser),
              new SqlParameter("@template_id",entity.template_id),
              new SqlParameter("@url",entity.url),
              new SqlParameter("@topcolor",entity.topcolor),
              new SqlParameter("@remarkText",entity.remarkText),
              new SqlParameter("@EarliestTime",entity.EarliestTime)
            };
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, paras);
        }
    }
}
